import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/foundation/key.dart';
import 'package:flutter/src/widgets/framework.dart';

class LoadingPage extends StatefulWidget {
  const LoadingPage({Key? key}) : super(key: key);

  @override
  State<LoadingPage> createState() => _LoadingPageState();
}

class _LoadingPageState extends State<LoadingPage> {
  String title = "加载中";
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("加载中...."),
      ),
      body: Center(
        child: LoadingWidget(title: title,),
      ),
    );
  }
}

class LoadingWidget extends StatefulWidget {
  final String title;
  const LoadingWidget({Key? key,required this.title}) : super(key: key);

  @override
  State<LoadingWidget> createState() => _LoadingWidgetState();
}

class _LoadingWidgetState extends State<LoadingWidget> {

  
  String _loadingTitle = "";
  int _index =1;
  Timer? _timer;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Future.delayed(Duration.zero,(){
      startTimer();

    });
  }
  void startTimer(){
    _timer = Timer.periodic(Duration(milliseconds:500), (timer){
        _index++;
        if(_index ==1){
          _loadingTitle=".";
        }else if(_index ==2){
          _loadingTitle = "..";
        }else if(_index == 3){
          _loadingTitle = "...";
          _index = 0;
        }
        setState(() {
          
        });
    });
  }
  void stopTimer(){
    if(_timer != null && _timer!.isActive){
      _timer?.cancel();
      _timer = null;
    }
  }

  @override
  void dispose() {
    // TODO: implement dispose
    stopTimer();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children:[
          CupertinoActivityIndicator(),
          SizedBox(width: 12),
          Text(
            "${widget.title} $_loadingTitle"
            )

        ]
      ),
    );
    
  }
}